/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.bl.RegisterBLLocal;
import mx.itschapala.dsw2.modelo.Register;

/**
 *
 * @author User
 */
@ManagedBean
@SessionScoped
public class UsuarioBean {
    @EJB
    private RegisterBLLocal registerBL;
    
    List<Register> lista;
    private Register user;
    private String accion;
    private int n;
    

    /**
     * Creates a new instance of UsuarioBean
     */
    public UsuarioBean() {
        user=new Register();
    }

    public List<Register> getLista() {
        lista=registerBL.getList();
        return lista;
    }

    public Register getUser() {
        return user;
    }

    public void setUser(Register user) {
        this.user = user;
    }
    
    
    /////////////////////////////////////////////////
    
    //-----------------Acciones------------------------------
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
       registerBL.register(user);
        }
        else
        if(accion.equalsIgnoreCase("Editar")){
           registerBL.modify(user);
        }
        else if(accion.equalsIgnoreCase("Eliminar")){
            registerBL.delete(user);
            System.out.println("jjjjj");
         
         
        }
        accion="";
    return "Usuario";
    }
    
    
   public String limpiarAccion(){
   user=new Register();
   return "TrabajadorCrearEditar";
    }
//-----------------Listener------------------------------

public void prepararNuevo(ActionEvent evt){
user=new Register();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    user=lista.get(n);
    accion="Editar";
}     


public void prepararEliminar(ActionEvent evt)   
{
    int x;
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    user=lista.get(n);
   
    
    accion="Eliminar";
}    


    
}
